package com.situ.water_BE.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.util.ChineseToFirstLetterUtil;
import com.situ.util.addBe_History;
import com.situ.util.getBeOrderNo;
import com.situ.water_BE.mapper.*;
import com.situ.water_BE.pojo.BeFormula;
import com.situ.water_BE.pojo.BeOrder;
import com.situ.water_BE.pojo.BeOrderuser;
import com.situ.water_BE.service.BeOrderuserService;
import com.situ.water_user.mapper.UsFormulaMapper;
import com.situ.water_user.mapper.UsMeterMapper;
import com.situ.water_user.mapper.UsUserMapper;
import com.situ.water_user.pojo.UsFormula;
import com.situ.water_user.pojo.UsMeter;
import com.situ.water_user.pojo.UsUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Angel
 * @since 2020-12-01
 */
@Service
public class BeOrderuserServiceImpl extends ServiceImpl<BeOrderuserMapper, BeOrderuser> implements BeOrderuserService {
    @Autowired
    private BeOrderuserMapper dao;
    @Autowired
    private BeOrderMapper beOrderDao;
    @Autowired
    private BeFlowMapper beFlowDao;
    @Autowired
    private BeHistoryMapper beHistoryDao;
    @Autowired
    private UsUserMapper usUserDao;
    @Autowired
    private UsMeterMapper usMeterDao;
    @Autowired
    private UsFormulaMapper usFormulaDao;
    @Autowired
    private BeFormulaMapper beFormulaDao;

    @Override
    public int addBE_OrderUser(BeOrderuser list, String orderId) {
        list.setOrderNo(orderId);
        int i = dao.insert(list);
        return i;
    }

    @Override
    public int addBE_OrderUser2(List<BeOrderuser> beOrderusers, String no) {
        int h = 0;
        if (beOrderusers != null) {
            List<BeOrderuser> orderNo = dao.selectList(new QueryWrapper<BeOrderuser>().
                    eq("OrderNo", no));
            for (int i = 0; i < beOrderusers.size(); i++) {
                beOrderusers.get(i).setId(orderNo.get(i).getId());
                h += dao.updateById(beOrderusers.get(i));
            }
        }
        return h;
    }

    @Override
    public List<BeOrderuser> queryId(String id) {
        // Wrapper
        QueryWrapper<BeOrderuser> wrapper = new QueryWrapper<BeOrderuser>();
        wrapper.eq(true, "orderNo", id);
        return dao.selectList(wrapper);
    }

    @Override
    public int updateBe__payForm(List<BeOrderuser> userList, BeOrder beOrder, Integer sessionId, Integer pd, Integer buZou,int history) {
        beOrder.setStepID(buZou);
        beOrder.setLastEditEmp(sessionId);
        beOrder.setLastEditDate(new Date());
        beOrder.setPayDate(new Date());
        int i = beOrderDao.updateById(beOrder);
        if (i >= 1) {
            int insert = beHistoryDao.insert(addBe_History.add(beOrder.getOrderNo(),
                    history, sessionId, pd));
            List<BeOrderuser> orderUsers = dao.selectList(new QueryWrapper<BeOrderuser>().
                    eq(true, "orderNo", beOrder.getOrderNo()));
            if (insert >= 1) {
                for (int j = 0; j < userList.size(); j++) {
                    dao.update(userList.get(j), new UpdateWrapper<BeOrderuser>().eq("id",
                            orderUsers.get(j).getId()));
                }
            }
            return insert;
        }
        return 0;
    }

    @Override
    public int updateBe__payForm2(List<BeOrderuser> userList, BeOrder beOrder,
                                  Integer sessionId, String lx, Integer buZou, Boolean bool) {
        beOrder.setStepID(beFlowDao.queryId(lx, buZou));
        beOrder.setLastEditEmp(sessionId);
        beOrder.setLastEditDate(new Date());
        if (bool) {
            beOrder.setPayDate(new Date());
        }
        int i = beOrderDao.updateById(beOrder);
        if (i >= 1) {
            List<BeOrderuser> orderUsers = dao.selectList(new QueryWrapper<BeOrderuser>().
                    eq(true, "orderNo", beOrder.getOrderNo()));
            for (int j = 0; j < userList.size(); j++) {
                dao.update(userList.get(j), new UpdateWrapper<BeOrderuser>().
                        eq("id", orderUsers.get(j).getId()));
            }
        }
        return i;
    }

    @Override
    @Transactional
    public int updateBe__payForm3(List<BeOrderuser> userList, BeOrder beOrder,
                                  Integer sessionId, Integer buZou, Boolean bool, String lx) {
        if (bool == false) {
            beOrder.setStepID(buZou);
            beOrder.setLastEditEmp(sessionId);
            beOrder.setLastEditDate(new Date());
            int i = beOrderDao.updateById(beOrder);
            //获取最后一行的用户编号
            //转换成自己的格式
            System.out.println(dao.queryUserNo());
            if (i >= 1) {
                List<BeOrderuser> orderUsers = dao.selectList(new QueryWrapper<BeOrderuser>().
                        eq(true, "orderNo", beOrder.getOrderNo()));
                for (int j = 0; j < userList.size(); j++) {
                    dao.update(userList.get(j), new UpdateWrapper<BeOrderuser>().
                            eq("id", orderUsers.get(j).getId()));
                }
            }
            return i;
        } else {
            beOrder.setStepID(buZou);
            beOrder.setLastEditEmp(sessionId);
            beOrder.setLastEditDate(new Date());
            beOrder.setCompleteDate(new Date());
            beOrder.setCompleteEmp(sessionId);
            int k = beOrderDao.updateById(beOrder);
            //获取最后一行的用户编号
            String s = beOrderDao.queryAreaId(beOrder.getOrderNo());
            //转换成自己的格式
            List<String> userNo = getBeOrderNo.getUserNo(s.substring(0, 2), s, userList.size());
            System.out.println(dao.queryUserNo());
            if (k >= 1) {
                List<BeOrderuser> orderUsers = dao.selectList(new QueryWrapper<BeOrderuser>().
                        eq(true, "orderNo", beOrder.getOrderNo()));
                for (int j = 0; j < userList.size(); j++) {
                    userList.get(j).setUserNo(userNo.get(j));
                    dao.update(userList.get(j), new UpdateWrapper<BeOrderuser>().
                            eq("id", orderUsers.get(j).getId()));
                }
                List<BeOrderuser> maps = dao.selectList(new QueryWrapper<BeOrderuser>().
                        eq("OrderNo", beOrder.getOrderNo()));
                BeOrder orderNo = beOrderDao.selectOne(new QueryWrapper<BeOrder>().
                        eq("OrderNo", beOrder.getOrderNo()));
                System.out.println(maps);
                int h = 0;
                for (int i = 0; i < maps.size(); i++) {
                    if ("3".equals(lx)) {
                        UsUser us = new UsUser();
                        us.setUserno(orderNo.getOldUserNo());
                        us.setUsername(maps.get(i).getUserName());
                        us.setPhone(maps.get(i).getPhone());
                        us.setSmsphone(maps.get(i).getSMSPhone());
                        us.setAddress(maps.get(i).getAddress());
                        h = usUserDao.updateById(us);
                    } else if ("4".equals(lx)) {
                        UsUser us = new UsUser();
                        us.setUserno(orderNo.getOldUserNo());
                        us.setPayType(orderNo.getPayType());
                        us.setBankName(orderNo.getBankCompany());
                        us.setBankNum(orderNo.getBankNum());
                        h = usUserDao.updateById(us);
                    } else if ("5".equals(lx)) {
                        UsMeter byId = usMeterDao.selectOne(new QueryWrapper<UsMeter>().
                                eq("UserNo", orderNo.getOldUserNo()).ne("Disabled", true));
                        UsMeter meter = new UsMeter();
                        System.out.println(byId);
                        meter.setMeterNo(getBeOrderNo.getMeterNo(byId.getUserNo(), byId.getMeterNo(), lx));
                        meter.setUserNo(byId.getUserNo());
                        meter.setMeterName(maps.get(i).getMeterName());
                        meter.setMeterTypeID(maps.get(i).getMeterTypeID());
                        meter.setMaxValue(maps.get(i).getMaxValue());
                        meter.setStartValue(maps.get(i).getStartValue());
                        meter.setSetupDate(orderNo.getProjectDate2());
                        meter.setFactory(maps.get(i).getMeterFactory());
                        meter.setDisabled(false);
                        h += usMeterDao.insert(meter);
                        byId.setDisabled(true);
                        h += usMeterDao.updateById(byId);
                    } else if ("6".equals(lx)) {
                        UsUser usUser = usUserDao.selectOne(new QueryWrapper<UsUser>().
                                eq("UserNo", orderNo.getOldUserNo()).ne("Disabled", true));
                        usUser.setFormula(maps.get(i).getFormula());
                        h += usUserDao.updateById(usUser);
                    } else if ("7".equals(lx)) {
                        UsUser usUser = usUserDao.selectOne(new QueryWrapper<UsUser>().
                                eq("UserNo", orderNo.getOldUserNo()).ne("Disabled", true));
                        usUser.setDisabled(true);
                        h += usUserDao.updateById(usUser);
                        UsMeter meter = usMeterDao.selectOne(new QueryWrapper<UsMeter>().
                                eq("UserNo", orderNo.getOldUserNo()).ne("Disabled", true));
                        meter.setDisabled(true);
                        h += usMeterDao.updateById(meter);
                    } else {
                        UsUser us = new UsUser();
                        us.setUserno(maps.get(i).getUserNo());
                        us.setUsername(maps.get(i).getUserName());
                        us.setAbc(ChineseToFirstLetterUtil.ChineseToFirstLetter(maps.get(i).getUserName()));
                        us.setCreatedate(new Date());
                        us.setAddress(maps.get(i).getAddress());
                        us.setPhone(maps.get(i).getPhone());
                        us.setSmsphone(maps.get(i).getSMSPhone());
                        us.setAreaID(orderNo.getAreaID());
                        us.setUserType(orderNo.getUserType());
                        us.setPayType("坐收");
                        us.setFormula(maps.get(i).getFormula());
                        us.setUserMoney(maps.get(i).getProjectMoney() - maps.get(i).getRealMoney());
                        us.setMeterID(maps.get(i).getMeterTypeID());
                        us.setDocNum(maps.get(i).getDocNum());
                        us.setContractNum(maps.get(i).getContractNum());
                        us.setContractDate(orderNo.getPayDate());
                        us.setVolumeID(0);
                        us.setDisabled(false);
                        h += usUserDao.insert(us);
                        if (h >= 1) {
                            h = 0;
                            UsMeter meter = new UsMeter();
                            meter.setMeterNo(getBeOrderNo.getMeterNo(maps.get(i).getUserNo(), "", lx));
                            meter.setUserNo(maps.get(i).getUserNo());
                            meter.setMeterName(maps.get(i).getMeterName());
                            meter.setMeterTypeID(maps.get(i).getMeterTypeID());
                            meter.setMaxValue(maps.get(i).getMaxValue());
                            meter.setStartValue(maps.get(i).getStartValue());
                            meter.setSetupDate(orderNo.getProjectDate2());
                            meter.setFactory(maps.get(i).getMeterFactory());
                            meter.setDisabled(false);
                            h += usMeterDao.insert(meter);
                        }
                        if (h >= 1) {
                            h = 0;
                            List<BeFormula> beFormulas = beFormulaDao.selectList(new QueryWrapper<BeFormula>().
                                    eq("OrderUserID", maps.get(i).getId()));
                            for (int j = 0; j < beFormulas.size(); j++) {
                                UsFormula usFormula = new UsFormula();
                                usFormula.setUserNo(maps.get(i).getUserNo());
                                usFormula.setWaterTypeCode(beFormulas.get(i).getWaterTypeCode());
                                usFormula.setAllotText(beFormulas.get(i).getAllotText());
                                usFormula.setAllotType(beFormulas.get(i).getAllotType());
                                if (beFormulas.get(i).getAllotValue() != null && !"".equals(beFormulas.get(i).getAllotValue())) {
                                    usFormula.setAllotValue(beFormulas.get(i).getAllotValue());
                                }
                                h += usFormulaDao.insert(usFormula);
                            }
                        }
                    }
                }
                if ("2".equals(lx)) {
                    UsUser userNo1 = usUserDao.selectOne(new QueryWrapper<UsUser>().
                            eq("UserNo", orderNo.getOldUserNo()));
                    System.out.println(userNo1);
                    UsUser usUser = new UsUser();
                    usUser.setUserno(userNo1.getUserno());
                    usUser.setDisabled(true);
                    h = usUserDao.updateById(usUser);
                }
                System.out.println(h);
                return h;
            }
            return 1;
        }
    }

    @Override
    public String queryUserNo() {
        return dao.queryUserNo();
    }

    @Override
    public String queryContractNum() {
        return dao.getContractNum();
    }

    @Override
    public int addOrderUser(String order, String docNum, BeOrder beOrder) {
        BeOrderuser user = new BeOrderuser();
        user.setOrderNo(order);
        user.setUserName(beOrder.getUserName());
        user.setPhone(beOrder.getPhone());
        user.setSMSPhone(beOrder.getPhone());
        user.setAddress(beOrder.getAddress());
        user.setDocNum(docNum);
        return dao.insert(user);
    }

    @Override
    public List<Map> queryAllOrderAndOrderUser(String orderNo) {
        return dao.queryAllOrderAndOrderUser(orderNo);
    }

}
